package com.datamation.swadeshi.control.data;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.datamation.swadeshi.model.Expense;
import com.datamation.swadeshi.model.Reason;

public class ExpenseDS {
	private SQLiteDatabase dB;
	private DatabaseHelper dbHelper;
	Context context;
	
	private String TAG="swadeshi";
	
	
	public ExpenseDS (Context context){
		this.context = context;
		dbHelper = new DatabaseHelper(context);
	}
	
	public void open() throws SQLException {
		dB = dbHelper.getWritableDatabase();
	}
	
	
	/*
	 * insert code
	 */
	@SuppressWarnings("static-access")
	public int createOrUpdateFExpense(ArrayList<Expense>  list) {
		
		int count =0;
		
		if(dB == null){
			open();
		}else if(!dB.isOpen()){
			open();
		}
		Cursor cursor = null;
		
	try{
		
    for (Expense expense : list) {
    		ContentValues values = new ContentValues();
			
			values.put(dbHelper.FEXPENSE_CODE, 	   expense.getFEXPENSE_CODE());
			values.put(dbHelper.FEXPENSE_GRP_CODE, expense.getFEXPENSE_GRP_CODE());
			values.put(dbHelper.FEXPENSE_NAME,     expense.getFEXPENSE_NAME());
			values.put(dbHelper.FEXPENSE_RECORDID, expense.getFEXPENSE_RECORDID());
			values.put(dbHelper.FEXPENSE_ADD_MACH, expense.getFEXPENSE_ADD_MACH());
			values.put(dbHelper.FEXPENSE_STATUS,   expense.getFEXPENSE_STATUS());
			values.put(dbHelper.FEXPENSE_ADD_USER, expense.getFEXPENSE_ADD_USER());
			values.put(dbHelper.FEXPENSE_ADD_DATE, expense.getFEXPENSE_ADD_DATE());
									
			count = (int) dB.insert(dbHelper.TABLE_FEXPENSE, null, values);

			}
		}catch (Exception e) {
		
			Log.v(TAG+" Exception", e.toString());
	
		}finally {  
			if (cursor !=null) {
				cursor.close();
			}
			dB.close();
		}
		return count;
				
	}
	
	/*
	 * delete code
	 */
	@SuppressWarnings("static-access")
	public int deleteAll(){
		
		int count =0;
		
		if(dB == null){
			open();
		}else if(!dB.isOpen()){
			open();
		}
		Cursor cursor = null;
		try{
			
			cursor = dB.rawQuery("SELECT * FROM " + dbHelper.TABLE_FEXPENSE, null);
			count =cursor.getCount();
			if(count>0){
				int success = dB.delete(dbHelper.TABLE_FEXPENSE, null, null);
				Log.v("Success", success+"");
			}
		}catch (Exception e){

			Log.v(TAG+" Exception", e.toString());
			
		}finally{  
			if (cursor !=null) {
				cursor.close();
			}
			dB.close();
		}
		
		return count;
		
	}
	
	public ArrayList<Expense> getAllExpense(String excode) {
		if (dB == null) {
			open();
		} else if (!dB.isOpen()) {
			open();
		}
		
		ArrayList<Expense> list = new ArrayList<Expense>();
		
		String selectQuery = "SELECT * FROM " + dbHelper.TABLE_FEXPENSE +" WHERE "+dbHelper.FEXPENSE_GRP_CODE+"='"+excode+"'";
		
		Cursor cursor = dB.rawQuery(selectQuery, null);
		while(cursor.moveToNext()){
			
			Expense expense=new Expense();
		
			
			expense.setFEXPENSE_CODE(cursor.getString(cursor.getColumnIndex(dbHelper.FEXPENSE_CODE)));
			expense.setFEXPENSE_NAME(cursor.getString(cursor.getColumnIndex(dbHelper.FEXPENSE_NAME)));
		
							
			list.add(expense);
			
		}
		
		return list;
	}
	
	
	public ArrayList<Expense> getExpDetails(String searchword) 
	{
		if (dB == null) {
			open();
		} else if (!dB.isOpen()) {
			open();
		}
		
		ArrayList<Expense> Itemname = new ArrayList<Expense>();

		String selectQuery = "select * from fExpense where expgrpcode='GCR001' AND ReaCode LIKE '%"+searchword+"%' OR ExpName LIKE '%"+searchword+"%' ";
		
		Cursor cursor = null;
		cursor = dB.rawQuery(selectQuery, null);
		
		while(cursor.moveToNext())
		{
			Expense expense=new Expense();
			
			expense.setFEXPENSE_NAME(cursor.getString(cursor.getColumnIndex(dbHelper.FEXPENSE_NAME)));
			expense.setFEXPENSE_CODE(cursor.getString(cursor.getColumnIndex(dbHelper.FEXPENSE_CODE)));
			Itemname.add(expense);
		}
		
		return Itemname;
	}
	
	
}
